package com.ruoyi.buy.service.impl;

import com.ruoyi.buy.domain.entity.PO.*;
import com.ruoyi.buy.domain.entity.inv0005.Inv0005InvDataDto;
import com.ruoyi.buy.domain.entity.inv0008.Inv0008OutInvDataDto;
import com.ruoyi.buy.domain.entity.inv0008.Inv0008UpdateByIdVO;
import com.ruoyi.buy.mapper.PO.*;
import com.ruoyi.buy.service.ICommonService;
import com.ruoyi.buy.service.IInvBackboundBillService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 退库单Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-01-03
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class InvBackboundBillServiceImpl implements IInvBackboundBillService
{
    @Resource
    private InvBackboundBillMapper invBackboundBillMapper;

    @Resource
    private ICommonService commonService;

    @Resource
    private InvBackboundBillItemMapper invBackboundBillItemMapper;

    @Resource
    private InvInventoryInfoMapper invInventoryInfoMapper;

    @Resource
    InvTransforminvBillMapper invTransforminvBillMapper;

    @Resource
    InvTransforminvBillItemMapper invTransforminvBillItemMapper;

    /**
     * 查询退库单
     * 
     * @param id 退库单主键
     * @return 退库单
     */
    @Override
    public Inv0008UpdateByIdVO selectInvBackboundBillById(Long id)
    {
        Inv0008UpdateByIdVO result = new Inv0008UpdateByIdVO();
        InvBackboundBill invBackboundBill = invBackboundBillMapper.selectInvBackboundBillById(id);
        result.setInvBackboundBill(invBackboundBill);

        InvBackboundBillItem invBackboundBillItem = new InvBackboundBillItem();
        invBackboundBillItem.setnMainId(id);
        List<InvBackboundBillItem> list = this.invBackboundBillItemMapper.selectInvBackboundBillItemList(invBackboundBillItem);
        result.setInvOutboundBillItemList(list);
        return result;
    }

    /**
     * 查询退库单列表
     * 
     * @param invBackboundBill 退库单
     * @return 退库单
     */
    @Override
    public List<InvBackboundBill> selectInvBackboundBillList(InvBackboundBill invBackboundBill)
    {
        return invBackboundBillMapper.selectInvBackboundBillList(invBackboundBill);
    }

    /**
     * 新增退库单
     * 
     * @param para 退库单
     * @return 结果
     */
    @Override
    public int insertInvBackboundBill(Inv0008UpdateByIdVO para)
    {
        //先增主表
        InvBackboundBill invBackboundBill = para.getInvBackboundBill();
        invBackboundBill.setCreateTime(DateUtils.getNowDate());
        invBackboundBill.setIotype("I");
        invBackboundBill.setvType("ITK");
        invBackboundBill.setStatus("0");
        invBackboundBill.setvBillNo(commonService.getNo("TK"));
        invBackboundBillMapper.insertInvBackboundBill(invBackboundBill);
        List<InvBackboundBill> list = this.invBackboundBillMapper.selectInvBackboundBillList(invBackboundBill);
        Long nMainId = list.get(0).getId();

        List<InvBackboundBillItem> invBackboundBillItems = para.getInvOutboundBillItemList();
        //再子表
        for (InvBackboundBillItem item: invBackboundBillItems
        ) {
            item.setnMainId(nMainId);
            item.setCreateTime(DateUtils.getNowDate());
            this.invBackboundBillItemMapper.insertInvBackboundBillItem(item);
        }
        return 1;
    }

    /**
     * 修改退库单
     * 
     * @param para 退库单
     * @return 结果
     */
    @Override
    public int updateInvBackboundBill(Inv0008UpdateByIdVO para)
    {
        InvBackboundBill invBackboundBill = para.getInvBackboundBill();
        invBackboundBill.setUpdateTime(DateUtils.getNowDate());
        invBackboundBillMapper.updateInvBackboundBill(invBackboundBill);

        Long id = invBackboundBill.getId();
        //再子表
        List<InvBackboundBillItem> invBackboundBillItemList = para.getInvOutboundBillItemList();
        for (InvBackboundBillItem item: invBackboundBillItemList
        ) {
            if (item.getId() == null || item.getId() == 0){
                item.setnMainId(id);
                item.setCreateTime(DateUtils.getNowDate());
                this.invBackboundBillItemMapper.insertInvBackboundBillItem(item);
            }else{
                InvBackboundBillItem search = new InvBackboundBillItem();
                search = this.invBackboundBillItemMapper.selectInvBackboundBillItemById(item.getId());
                if(search.getnQty() != item.getnQty()){
                    this.invBackboundBillItemMapper.updateInvBackboundBillItem(item);
                }
            }

        }
        return 1;
    }

    /**
     * 批量删除退库单
     * 
     * @param ids 需要删除的退库单主键
     * @return 结果
     */
    @Override
    public int deleteInvBackboundBillByIds(Long[] ids)
    {
        return invBackboundBillMapper.deleteInvBackboundBillByIds(ids);
    }

    /**
     * 删除退库单信息
     * 
     * @param id 退库单主键
     * @return 结果
     */
    @Override
    public int deleteInvBackboundBillById(Long id)
    {
        return invBackboundBillMapper.deleteInvBackboundBillById(id);
    }

    @Override
    public List<Inv0008OutInvDataDto> getOutInvData(Inv0005InvDataDto para) {
        return this.invBackboundBillMapper.getOutInvData(para);
    }

    @Override
    public int deleteInvBackboundBillItemById(InvBackboundBillItem para) {
        return invBackboundBillItemMapper.deleteInvBackboundBillItemById(para.getId());
    }

    @Override
    public String checkInvQty(Inv0005InvDataDto para) {
        BigDecimal nQty = para.getnQty();
        String message = "成功";
        para.setnQty(null);
        List<Inv0008OutInvDataDto> list = this.invBackboundBillMapper.getOutInvData(para);
        if(list.size() == 0){
            message = "该物料全部退库或不存在，请确认";
        }else{

            Inv0008OutInvDataDto nowData = list.get(0);
            if(nQty.compareTo(nowData.getnQty())>0){
                message = "退库数量已超过出库数量，请确认";
            }
        }
        return message;
    }

    @Override
    public String executeBack(Inv0008UpdateByIdVO para) throws Exception {
        String result = "成功";
        InvBackboundBill invBackboundBill = para.getInvBackboundBill();
        //库存数量校验
        List<InvBackboundBillItem> invBackboundBillItemList = para.getInvOutboundBillItemList();
        for (InvBackboundBillItem item:invBackboundBillItemList
        ) {
            BigDecimal nQty = item.getnQty();
            Inv0005InvDataDto inv0005InvDataDto = new Inv0005InvDataDto();
            BeanUtils.copyBeanProp(inv0005InvDataDto,item);
            inv0005InvDataDto.setnQty(null);
            inv0005InvDataDto.setvProjectNo(invBackboundBill.getvProjectNo());
            List<Inv0008OutInvDataDto> nowDataList = this.invBackboundBillMapper.getOutInvData(inv0005InvDataDto);

            if(nowDataList.isEmpty()){
                result = "物料:"+item.getvSpaName()+"库存数量不足，请确认";
                return result;
            }else{
                Inv0008OutInvDataDto nowData =nowDataList.get(0);
                if(nQty.compareTo(nowData.getnQty())>0){
                    result = "物料:"+item.getvSpaName()+"库存数量不足，请确认";
                    return result;
                }
            }

        }

        //保存主子表信息

        invBackboundBill.setStatus("1");
        para.setInvBackboundBill(invBackboundBill);
        this.updateInvBackboundBill(para);

        //拆先入先出
        List<InvBackboundBillItem> list = new ArrayList<>();
        for (InvBackboundBillItem item:invBackboundBillItemList
        ) {
            //先查这个项目这个件的出库信息
            Map<String,String> map = new HashMap<>();
            map.put("vProjectNo",invBackboundBill.getvProjectNo());
            map.put("vSpaName",item.getvSpaName());
            map.put("vSpaModel",item.getvSpaModel());
            map.put("vSpaBrand",item.getvSpaBrand());
            List<InvInoutBillItem> invInoutBillItems = this.invBackboundBillMapper.getOutInvDataByVSpa(map);
            
            BigDecimal nQty = item.getnQty();
            for (InvInoutBillItem inoutBillItem:invInoutBillItems
                 ) {
                if (nQty.compareTo(BigDecimal.ZERO)>0){
                    InvBackboundBillItem invBackboundBillItem = new InvBackboundBillItem();
                    BeanUtils.copyBeanProp(invBackboundBillItem,item);
                    if (nQty.compareTo(inoutBillItem.getnQty()) > 0){
                        invBackboundBillItem.setnQty(inoutBillItem.getnQty());
                        nQty = nQty.subtract(inoutBillItem.getnQty());
                    }else{
                        invBackboundBillItem.setnQty(nQty);
                        nQty = nQty.subtract(nQty);
                    }
                    invBackboundBillItem.setvLotNo(inoutBillItem.getvLotNo());
                    invBackboundBillItem.setnPrice(inoutBillItem.getnPrice());
                    list.add(invBackboundBillItem);
                }
            }
        }
        //改库存
        commonService.changeInventory(para.getInvBackboundBill(),new ArrayList<>(list),"TK");
        return result;
    }

    private void dealTurnToYiBanInventory(Inv0008UpdateByIdVO para){
        //这个方法是一般库存转项目库存
        InvTransforminvBill invTransforminvBill = new InvTransforminvBill();
        invTransforminvBill.setIsTransform("N");//一般库存转项目
        invTransforminvBill.setvBillNo(commonService.getNo("HZ"));
        invTransforminvBill.setvProjectNo(para.getInvBackboundBill().getvProjectNo());
        invTransforminvBill.setCreateBy(commonService.getUsername());
        invTransforminvBill.setCreateTime(DateUtils.getNowDate());
        invTransforminvBillMapper.insertInvTransforminvBill(invTransforminvBill);


        for (InvBackboundBillItem item:para.getInvOutboundBillItemList()
             ) {

        }
    }
}
